package org.systemsbiology.addama.sequencing.util;

import java.io.*;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: jlin
 * Date: Sep 8, 2010
 * Time: 9:39:20 AM
 * To change this template use File | Settings | File Templates.
 */
public class CathyUsda2Tsv {


    public static void main(String[] args) {

        /*String test = "YMULTI_RAD_27,,,,YO 472,3,GGCC";
        String[] tokens = test.split(",");
        System.out.println("tokens:" + tokens);
         */

        CathyUsda2Tsv c = new CathyUsda2Tsv();
        String keyLine = "";
        String strLine = "";

        try {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream("/local/python/cathy.usda");
            StringBuffer sb = new StringBuffer();
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            //Read File Line By Line
            System.out.println(new Date());
            // Create file
            FileWriter ofstream = new FileWriter("/local/python/cathy.usda.tsv");
            BufferedWriter out = new BufferedWriter(ofstream);
            out.write("NRRLKey\tAccession\tIsolatedFrom\tSubstrateLocation\tIdentification\tComments\t\n");
            while ((strLine = br.readLine()) != null) {

                strLine = strLine.trim();
                //System.out.println("Line:" + strLine);
                if (!strLine.equals("")) {
                    sb.append(strLine + "\n");
                } else {
                    System.out.println("Buffer:" + sb);

                    String[] lines = sb.toString().split("\n");
                    if (lines.length >= 3) {
                        //keyLine = lines[0];
                        CData dt = new CData();

                        for (int i = 0; i < lines.length; i++) {

                            if (!lines[i].equals("")) {
                                if (lines[i].startsWith("NRRL ")) {
                                    String key = lines[i].split(" ")[1];
                                    dt.setKey(key);
                                }
                                String tk = lines[i].split(":")[0];
                                if (tk.contains(" ")) {
                                    tk = tk.split(" ")[0];
                                }
                                if (tk.equals("Accession")) {
                                    dt.setAccession(lines[i].split(":")[1]);
                                }
                                if (tk.equals("Isolated")) {
                                    dt.setIsolate(lines[i].split(":")[1]);
                                }
                                if (tk.equals("Substrate")) {
                                    dt.setLocation(lines[i].split(":")[1]);
                                }
                                if (tk.equals("Identified")) {
                                    dt.setIdentification(lines[i].split(":")[1]);
                                }
                                if (tk.equals("Comments")) {
                                    dt.setComments(lines[i].split(":")[1]);
                                }

                                /*CData dt = new CData(key, lines[1].split(" ")[1],
                                    lines[2].split(" ")[1],lines[3].split(" ")[1],lines[4].split(" ")[1],
                                    lines[5].split(" ")[1]);
                                */

                            }
                            if (i == (lines.length - 1)) {
                                out.write(dt.toString());                                    
                                System.out.println(dt);
                            }
                        }
                        sb.setLength(0);
                    }
                }
                // Print the content on the console
                //System.out.println(strLine);
            }
            //Close the input stream
            in.close();
            out.close();

        } catch (Exception e) {//Catch exception if any
            System.out.println("Error: " + e.getMessage() + " KeyLine:" + keyLine + " line:" + strLine);
            e.printStackTrace();
        }


    }
}
